
// Set up Variables
var interval = 10000;
var fadeInLength = 2000;
var fadeOutLength = 2000;
var betweenFades = interval - (fadeInLength + fadeOutLength);

// Init
var screen_effect = {};
var images = new Array();
var slideIndex = 0;



// Start Slideshow --------------------------------------------------------

screen_effect.startSlideshow = function () {
  
  // Load First Image
  var image = new Image();
  image.src = images[slideIndex];
  
  // Begin Animation when Image is Loaded
  image.onload = function() {
  
    // Setup Screen Effect
    $("body").wrapInner("<div id='not-Screen-Effect'></div>");
    $("body").prepend("<div id='Screen-Effect'></div>");
    
    // Change original background
    $("body").css("background-image", "none");
    $("body").css("background-color", "#000000");
    
    // Add Classes for Styling
    $(image).addClass("Screen-Effect-Still");
    $(image).addClass("Current-Screen-Effect-Still");
    
    // It begins at 0 opacity.
    $(image).fadeOut(0, function() {
    
      // At it to the Slideshow
      $('#Screen-Effect').append(image);
      
      // Fade it in
      $(image).fadeIn(fadeInLength, function() {
        // $.cookie("screenEffectSlide", slideIndex);
        slideIndex++;
        screen_effect.loadNextSlide(image);
      });
    });
  };
}



// Load Next Slide --------------------------------------------------------

screen_effect.loadNextSlide = function(current) {
  
  // init Image
  var nextImage = new Image();
  
  // Set up Flags
  var waitOver = false;
  var imageLoaded = false;

  // Loop to Beginning
  if (images[slideIndex] == null) {
    slideIndex = 0;
  }
  
  // Load the Image
  nextImage.src = images[slideIndex];
  
  // Change Slide once Loaded & Wait Over
  nextImage.onload = function() {
    $(nextImage).fadeOut(0, function() {
    
      // Add Classes for Styling
      $(nextImage).addClass("Screen-Effect-Still");
      $(nextImage).addClass("Next-Screen-Effect-Still");
      
      imageLoaded = true;
      if (waitOver) {
        screen_effect.changeSlide(current, nextImage);
      }
    });
  };

  // Wait at least the amount of time of betweenFades
  window.setTimeout(function() {
    waitOver = true;
    if (imageLoaded) {
      screen_effect.changeSlide(current, nextImage);
    }
  },
  betweenFades);
  
}



// Animate to Next Slide --------------------------------------------------------

screen_effect.changeSlide = function(current, next) {
    
  $('#Screen-Effect').append(next);

  $(current).fadeOut(fadeOutLength, function() {
    $(current).remove();
      
    // Change Next Slide to Current Slide
    $(next).removeClass("Next-Screen-Effect-Still");
    $(next).addClass("Current-Screen-Effect-Still");
    
    $(next).fadeIn(fadeInLength, function() {
      // $.cookie("screenEffectSlide", slideIndex);
      slideIndex++;
      screen_effect.loadNextSlide(next);
    });
  });
}

  
  
// JQuery -------------------------------------------------------

$(document).ready(function() {
    
    // get Cookie
    /*if ($.cookie('screenEffectSlide')) {
      slideIndex = $.cookie('screenEffectSlide');
    }*/
    
    // Get Image URLS from PHP
    Drupal.service('screen_effect.getImages', {reset: "false"}, function (status, data) {
      if (status == false) {
        alert("Could not load Images for Screen Effect");
      }
      else {
        for (i in data) {
          images.push(data[i]);
        }
        
        // call function
        screen_effect.startSlideshow();
      }
    });
  });